package com.wm.wmcrm.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wm.wmcrm.sys.entity.SysDept;
import com.wm.wmcrm.sys.mapper.SysDeptMapper;
import com.wm.wmcrm.sys.service.ISysDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wm
 * @since 2023-03-07
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {

    @Override
    public List<SysDept> queryList(String deptName) {
        if("".equals(deptName)){
            LambdaQueryWrapper<SysDept> lambdaQueryWrapper = new LambdaQueryWrapper<SysDept>()
                    .eq(SysDept::getParentId,0);
            List<SysDept> sysDeptList = this.baseMapper.selectList(lambdaQueryWrapper);
            for(SysDept sysDept:sysDeptList){
                LambdaQueryWrapper<SysDept> lambdaQueryWrapper1 = new LambdaQueryWrapper<SysDept>()
                        .eq(SysDept::getParentId,sysDept.getDeptId());
                List<SysDept> sysDeptC = this.baseMapper.selectList(lambdaQueryWrapper1);
                if (!sysDeptC.isEmpty()){
                    sysDept.setChildren(sysDeptC);
                }
            }
            return sysDeptList;
        }else {
            LambdaQueryWrapper<SysDept> lambdaQueryWrapper = new LambdaQueryWrapper<SysDept>()
                    .like(StringUtils.isNotBlank(deptName),SysDept::getDeptName,deptName);
            return this.baseMapper.selectList(lambdaQueryWrapper);
        }
    }
}
